#include "lobbys.h"

Lobbys::Lobbys(Server *server, QObject *parent) : QObject(parent)
{
    _server = server;
    _nextID = 1000;
}

Lobby* Lobbys::getLobby(int id){
    foreach (Lobby* l, _lobbyList) {
        if(l->getID() == id){
            return l;
        }
    }
    return NULL;
}

Lobby* Lobbys::getLobbyWithUser(User *user){
    foreach(Lobby *l,_lobbyList){
        if(l->containUser(user)) return l;
    }
    return NULL;
}

QList<Lobby*>& Lobbys::getLobbysList(){
    return _lobbyList;
}

int Lobbys::createLobby(LobbyType type,QString name, QString pass){
    _nextID = _nextID + 1;
    Lobby *lobby = new Lobby(_nextID,type,name,pass,this);
    _lobbyList.append(lobby);
    return _nextID;
}

bool Lobbys::deleteLobby(int id){
    foreach(Lobby *l,_lobbyList){
        if(l->getID()==id){
            _lobbyList.removeOne(l);
            return true;
        }
    }
    return false;
}

bool Lobbys::deleteLobby(QString name, QString pass){
    foreach(Lobby *l,_lobbyList){
        if(l->checkAuth(name,pass)){
            _lobbyList.removeOne(l);
            return true;
        }
    }
    return false;
}

bool Lobbys::addUserToLobby(int user, int lobby){
    User *u =_server->_connections->findUser(user);
    if(u == NULL) return false;
    foreach(Lobby* l,_lobbyList){
        if(l->getID() == lobby){
            if(!l->containUser(u)){
                l->userAdd(u);
            }
        }
    }
    return false;
}
